﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE_TOOL.DBAccess
{
	class ToolGroupDAO : BaseDAO
	{
		#region Basic Method

		public static DataTable GetAll()
		{
			return ExcuteProcedure(ToolConstants.StoreProcedure.ToolGroup.GetAll);
		}

		public static DataTable GetOne(string toolGroupId)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@ToolGroupID", SqlDbType.NVarChar, toolGroupId)
                    };
				return ExcuteProcedure(ToolConstants.StoreProcedure.ToolGroup.GetOne, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static ToolGroup LoadOne(string toolGroupId)
		{
			ToolGroup o = new ToolGroup();
			DataTable dt = GetOne(toolGroupId);
			if(dt.Rows.Count > 0)
			{
				o.ToolGroupID = dt.Rows[0][ToolConstants.Entities.ToolGroup.FieldName.ToolGroupID].ToString();
				o.ToolGroupName = dt.Rows[0][ToolConstants.Entities.ToolGroup.FieldName.ToolGroupName].ToString();

			}

			return o;
		}


		public static int Insert(ToolGroup o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@ToolGroupID", SqlDbType.NVarChar, o.ToolGroupID),
                        CreateParameter("@ToolGroupName", SqlDbType.NVarChar, o.ToolGroupName),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.ToolGroup.Insert, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static int Update(ToolGroup o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@TempID", SqlDbType.NVarChar, o.TempId),
                        CreateParameter("@ToolGroupID", SqlDbType.NVarChar, o.ToolGroupID),
                        CreateParameter("@ToolGroupName", SqlDbType.NVarChar, o.ToolGroupName),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.ToolGroup.Update, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static int Delete(ToolGroup o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@ToolGroupID", SqlDbType.NVarChar, o.ToolGroupID)
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.ToolGroup.Delete, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static DataTable GetTemplateTable()
		{
			DataTable table = new DataTable("333_ToolGroup");
			table.Columns.Add("ToolGroupID");
			table.Columns.Add("ToolGroupName");

			return table;
		}

		#endregion
	}
}
